//
//  FacilitatorLogonViewController.m
//  HQJFacilitator
//
//  Created by mymac on 2016/10/12.
//  Copyright © 2016年 Fujian first time iot technology investment co., LTD. All rights reserved.
//

#import "FacilitatorLogonViewController.h"
#import "pickerView.h"
#import "SUCCESSViewController.h"

@interface FacilitatorLogonViewController () <UIScrollViewDelegate,UITextFieldDelegate>

{
    
    NSArray *_typeAry;  // -- 服务商类型返回的数组
    NSArray *_provincesAry; // --- 省份
    NSArray *_cityAry; //  ---城市
    NSArray *_countyAry;  // ---区/县
    
    NSInteger _typeIndex; // 服务商点击的索引
    NSInteger _provincesIndex; // --- 省份
    NSInteger _cityIndex;   //  ---城市
    NSInteger _countyIndex; // ---区/县

    NSString * _transactorIDStr; // ---执行人id
    NSString * _servicerIDStr;   // ----服务人id
    NSString * _organiserIDStr;  // ----组织人id
    
    
    BOOL _isCounty; //  --区县是否是空
    
}
@property (nonatomic,strong) pickerView *pickView;
@property (nonatomic,assign)CGFloat offSizeScroll;
@property (nonatomic,strong)UITextField *mobileTextField,*nameTextField,*idTextField,*emailTextField,*addressTextField,*ReferrerTextField,*transactorTextField,*servicerTextField,*organiserTextField,*typeTextField,*provincesTextField,*cityTextField,*countyTextField;
@property (nonatomic,strong) UIScrollView *mainScrollView;
@property (nonatomic,strong)UIButton *logonButton;
@property (nonatomic,strong)UIImageView *ReferrerHeaderView,*transactorHeaderView,*servicerHeaderView,*organiserHeaderView;
@property (nonatomic,strong)UILabel *ReferrerLabel,*transactorLabel,*servicerLabel,*organiserLabel;

@property (nonatomic,assign)    BOOL isAllSignal; //--总信号

//@property (nonatomic,strong)
@end

@implementation FacilitatorLogonViewController
-(NSArray *)LabelTitleArrayisLabel:(BOOL)type {
    
    NSArray *LabelTitleArray;
        if (type== YES) {
              LabelTitleArray = @[@"电    话:",@"姓    名:",@"身份证:",@"邮    箱:",@"地    址:",@"引荐人:",@"执行人:",@"服务人:",@"组织人:"];
        } else {
              LabelTitleArray = @[@"电话号码",@"姓名",@"身份证号码",@"邮箱地址",@"联系地址",@"引荐人",@"执行人",@"服务人",@"组织人"];
        }
      
    
    
    
    return LabelTitleArray;
}


-(UIScrollView *)mainScrollView {
    if (!_mainScrollView) {
        _mainScrollView = [[UIScrollView alloc]init];
        _mainScrollView.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
        _mainScrollView.contentOffset = CGPointMake(0, 0);
        _mainScrollView.delegate = self;
        _mainScrollView.scrollsToTop = NO;
        _mainScrollView.contentSize = CGSizeMake(WIDTH, 966);
        _mainScrollView.backgroundColor= [UIColor groupTableViewBackgroundColor];
        //    mainScrollView.delegate = self;
        [self.view addSubview:_mainScrollView];

    }
    
    return _mainScrollView;
    
}

-(UIButton *)logonButton {
    
    if (!_logonButton) {
        _logonButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_logonButton bk_addEventHandler:^(id  _Nonnull sender) {
            NSLog(@"---点击了注册按钮");
        } forControlEvents:UIControlEventTouchUpInside];
        _logonButton.layer.masksToBounds = YES;
        _logonButton.layer.cornerRadius  = 5;
        [_logonButton setTitle:@"注册" forState:UIControlStateNormal];
        _logonButton.backgroundColor     = [ManagerEngine getColor:@"00ccb8"];
        [self.mainScrollView addSubview:_logonButton];

    }
    
    
    return _logonButton;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createUI];
    [self createSignal];
    
    
    [self firstRequst];
    
    self.title = @"服务商注册";
    _isCounty = NO;
    _isAllSignal = NO;
    // Do any additional setup after loading the view.
}

#pragma mark --
#pragma mark ---视图内容
-(void)createUI {
    
    for (NSInteger i=0; i<5; i++) {
        UILabel *label = [[UILabel alloc]init];
//        label.backgroundColor = [UIColor redColor];
        CGFloat labelSize = [ManagerEngine setTextWidthStr:[self LabelTitleArrayisLabel:YES][i] andFont:[UIFont systemFontOfSize:17]];
        label.text = [self LabelTitleArrayisLabel:YES][i];
        label.frame = CGRectMake(15, i%5*(20+45)+25, labelSize, 20);
        label.font = [UIFont systemFontOfSize:17];

        [self.mainScrollView addSubview:label];
        
        UITextField *textField = [[UITextField alloc]init];
        textField.tag = i;
        textField.frame = CGRectMake(15+labelSize+5, i%5*(44+20)+15, WIDTH-15*2-labelSize-5, 44);
        textField.delegate =self;
        textField.font = [UIFont systemFontOfSize:15];
        textField.borderStyle = UITextBorderStyleRoundedRect;
        textField.placeholder = [NSString stringWithFormat:@"请输入%@",[self LabelTitleArrayisLabel:NO][i]];
        textField.autocorrectionType = UITextAutocorrectionTypeNo;
        textField.backgroundColor = [UIColor whiteColor];
        [self.mainScrollView addSubview:textField];
        switch (textField.tag) {
            case 0:
                _mobileTextField = textField;
                textField.keyboardType = UIKeyboardTypeNumberPad;
                break;
            case 1:
                _nameTextField = textField;
                textField.textColor = [ManagerEngine getColor:@"999999"];
                 textField.keyboardType = UIKeyboardTypeDefault;
                break;
            case 2:
               _idTextField = textField;
                textField.textColor = [ManagerEngine getColor:@"999999"];
                textField.keyboardType = UIKeyboardTypeNumberPad;
                break;
            case 3:
                _emailTextField = textField;
               textField.keyboardType =  UIKeyboardTypeEmailAddress;
                break;
            case 4:
                _addressTextField = textField;
                textField.keyboardType = UIKeyboardTypeDefault;
                break;
            default:
                break;
        }
        
    }
    for (NSInteger i=0; i<4; i++) {
        UILabel *label = [[UILabel alloc]init];
//        label.backgroundColor = [UIColor blueColor];
        CGFloat labelSize = [ManagerEngine setTextWidthStr:[self LabelTitleArrayisLabel:YES][i+5] andFont:[UIFont systemFontOfSize:17]];

        label.frame = CGRectMake(15, i%4*(20+60)+35+_addressTextField.frame.origin.y+_addressTextField.frame.size.height, labelSize, 20);
        label.font = [UIFont systemFontOfSize:17];
        label.text = [self LabelTitleArrayisLabel:YES][i+5];
        
        [self.mainScrollView addSubview:label];
        
        UITextField *textField = [[UITextField alloc]init];
        textField.tag = i+5;
        textField.frame = CGRectMake(15+labelSize+5, i%5*(44+35)+25+_addressTextField.frame.origin.y+_addressTextField.frame.size.height, WIDTH-15*2-labelSize-5, 44);
        textField.borderStyle = UITextBorderStyleRoundedRect;
        textField.backgroundColor = [UIColor whiteColor];
        textField.placeholder = [NSString stringWithFormat:@"请输入%@手机号码",[self LabelTitleArrayisLabel:NO][i+5]];
        textField.delegate =self;
          textField.keyboardType = UIKeyboardTypeNumberPad;
        [_mainScrollView addSubview:textField];
        switch (textField.tag) {
            case 5:
                _ReferrerTextField = textField;
                textField.textColor = [ManagerEngine getColor:@"999999"];
                textField.text = [FileEngine filePathNameReadName:@"mobile" andstye:fileDefaultStyle];
                break;
            case 6:
                _transactorTextField = textField;
                break;
            case 7:
                _servicerTextField = textField;
                break;
            case 8:
                _organiserTextField = textField;
                break;
          
            default:
                break;
        }


                   }
#pragma mark --
#pragma mark ---小头像
    _ReferrerHeaderView = [[UIImageView alloc]init];
    [self headerView:_ReferrerHeaderView];
    
  _ReferrerLabel = [[UILabel alloc]init];
    [self headerLabel:_ReferrerLabel];
   
    _ReferrerLabel.text = [FileEngine filePathNameReadName:@"realname" andstye:fileDefaultStyle];
    
    CGFloat nameSize = [ManagerEngine setTextWidthStr:_ReferrerLabel.text andFont:[UIFont systemFontOfSize:15]];
    
//    HeaderView.frame = CGRectMake(WIDTH-15-nameSize.width-20-5,i%4*(20+60)+_ReferrerTextField.frame.origin.y+_ReferrerTextField.frame.size.height+5, 20, 20);
    _ReferrerHeaderView.sd_layout.leftSpaceToView(self.mainScrollView,WIDTH-15-nameSize-20-5).topSpaceToView(_ReferrerTextField,5).heightIs(20).widthIs(20);
    _ReferrerLabel.sd_layout.leftSpaceToView(_ReferrerHeaderView,5).topEqualToView(_ReferrerHeaderView).heightIs(20).widthIs(nameSize);
    _transactorHeaderView = [[UIImageView alloc]init];
    [self headerView:_transactorHeaderView];
    _transactorHeaderView.sd_layout.leftSpaceToView(self.mainScrollView,WIDTH-15-20-5).topSpaceToView(_transactorTextField,5).heightIs(20).widthIs(20);

    _transactorLabel = [[UILabel alloc]init];
    [self headerLabel:_transactorLabel];
    
    
    _servicerHeaderView = [[UIImageView alloc]init];
    [self headerView:_servicerHeaderView];
    _servicerHeaderView.sd_layout.leftSpaceToView(self.mainScrollView,WIDTH-15-20-5).topSpaceToView(_servicerTextField,5).heightIs(20).widthIs(20);
    
    _servicerLabel = [[UILabel alloc]init];
    [self headerLabel:_servicerLabel];
    
    
    _organiserHeaderView = [[UIImageView alloc]init];
    [self headerView:_organiserHeaderView];
    _organiserHeaderView.sd_layout.leftSpaceToView(self.mainScrollView,WIDTH-15-20-5).topSpaceToView(_organiserTextField,5).heightIs(20).widthIs(20);
    
    _organiserLabel = [[UILabel alloc]init];
    [self headerLabel:_organiserLabel];
    


    
#pragma mark --
#pragma mark ---服务商类型
    UILabel *facilitatorTypeLabel = [[UILabel alloc]init];
    facilitatorTypeLabel.text = @"服务商类型:";
    facilitatorTypeLabel.font = [UIFont systemFontOfSize:17];
    CGFloat typeSize = [ManagerEngine setTextWidthStr:@"服务商类型:" andFont:[UIFont systemFontOfSize:17]];
    facilitatorTypeLabel.frame = CGRectMake(15, _organiserTextField.frame.origin.y+_organiserTextField.frame.size.height+45, typeSize, 20);
    
    [_mainScrollView addSubview:facilitatorTypeLabel];
    
   _typeTextField = [[UITextField alloc]init];
    _typeTextField.backgroundColor = [UIColor whiteColor];
    _typeTextField.borderStyle = UITextBorderStyleRoundedRect;
    _typeTextField.delegate = self;
    [_mainScrollView addSubview:_typeTextField];
    _typeTextField.textColor = [ManagerEngine getColor:@"999999"];

    _typeTextField.sd_layout.leftSpaceToView(facilitatorTypeLabel,5).topSpaceToView(_organiserTextField,35).heightIs(44).widthIs(WIDTH-15*2-facilitatorTypeLabel.frame.size.width-5);
    _typeTextField.placeholder =@"请选择服务商类型";
    [self pickviewTextField:_typeTextField];
    
    
#pragma mark --
#pragma mark ---地区
    UILabel *regionLabel = [[UILabel alloc]init];
    
    regionLabel.text = @"地区:";

    regionLabel.font = [UIFont systemFontOfSize:17];
    CGFloat regionSize = [ManagerEngine setTextWidthStr:@"地区:" andFont:[UIFont systemFontOfSize:17]];
    regionLabel.frame = CGRectMake(15, facilitatorTypeLabel.frame.origin.y+facilitatorTypeLabel.frame.size.height+44, regionSize, 20);
    
    [self.mainScrollView addSubview:regionLabel];
    
    
#pragma mark --
#pragma mark ---地区下的三个TextField
    for (NSInteger i=0; i<3; i++) {
        
        UITextField *regionTextField = [[UITextField alloc]init];
        regionTextField.backgroundColor = [UIColor whiteColor];
        regionTextField.borderStyle = UITextBorderStyleRoundedRect;
        regionTextField.delegate = self;
        regionTextField.textColor = [ManagerEngine getColor:@"999999"];
//        UITapGestureRecognizer *tap = [UITapGestureRecognizer alloc]initWithTarget:se action:<#(nullable SEL)#>;
        regionTextField.tag = i;
        [self pickviewTextField:regionTextField];
        [_mainScrollView addSubview:regionTextField];
        regionTextField.frame = CGRectMake(regionSize+15+5,i%3*(44+20)+facilitatorTypeLabel.frame.origin.y+facilitatorTypeLabel.frame.size.height+30 , WIDTH-regionSize-15*2-5,44);
        if (regionTextField.tag == 0) {
            regionTextField.placeholder = @"省份";
            _provincesTextField = regionTextField;
        } else if (regionTextField.tag == 1) {
            regionTextField.placeholder = @"城市";
            _cityTextField = regionTextField;

        }else {
            regionTextField.placeholder = @"区/县";
            _countyTextField = regionTextField;
        }

        
    }
    


   
    
    
    
#pragma mark --
#pragma mark ---注册按钮
       self.logonButton.sd_layout.leftSpaceToView(self.mainScrollView,15).topSpaceToView(self.mainScrollView,self.mainScrollView.contentSize.height-15-44).heightIs(44).widthIs(WIDTH-15*2);
    
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (textField == _typeTextField ||textField == _provincesTextField ||textField == _cityTextField || textField == _countyTextField ||textField == _ReferrerTextField || textField ==_nameTextField || textField == _idTextField) {
        return NO;

    } else {
        
        return YES;
    }
}


#pragma mark --
#pragma mark ---输入框右边小箭头的封装方法
-(void)pickviewTextField:(UITextField *)text {
    
    
    @weakify(self);

    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn bk_addEventHandler:^(id  _Nonnull sender) {
            
            
            
//            HQJLog(@"选择了第个%ld\n并且点击的是%@",text.tag,text.placeholder);
            if ([text isEqual: _provincesTextField]){
                if ([_typeTextField.text isEqualToString:@""]) {
                    HQJLog(@"你没有选择服务商类型呦");
                }else {
                    if (text.frame.origin.y+text.frame.size.height-_offSizeScroll+100 >HEIGHT) {
                        CGFloat offset = text.frame.origin.y+text.frame.size.height-_offSizeScroll+100 - HEIGHT;
                        self.mainScrollView.contentOffset = CGPointMake(0, self.mainScrollView.contentOffset.y+offset+20);
                    }
                    [RequestEngine HQJFacilitatorRequestDetailsUrl:[NSString stringWithFormat:@"%@getProvince",AppOrg_URL] complete:^(NSDictionary *dic) {
                        _provincesAry = (NSArray *)dic[@"result"];
                        NSMutableArray *arrays = [NSMutableArray array];
                        for (NSInteger i=0; i<_provincesAry.count; i++) {
                            NSString *str = _provincesAry[i][@"area"];
                            
                            [arrays addObject:str];
                        }
                        _pickView = [[pickerView alloc]initWithFrame:CGRectMake(text.frame.origin.x, text.frame.origin.y+text.frame.size.height-_offSizeScroll, text.frame.size.width, 100) andTitleAry:arrays];
                        [_pickView showView];
                        [_pickView setSenderBlock:^(id sender) {
                            
//                            HQJLog(@"%@-=-=-",arrays[[sender integerValue]]);
                            if (![text.text isEqualToString:arrays[[sender integerValue]]]) {
                                self_weak_.cityTextField.text = @"";
                                self_weak_.countyTextField.text = @"";
                            }
                            text.text = [NSString stringWithFormat:@"%@",arrays[[sender integerValue]]];
                            _provincesIndex = [sender integerValue];
                         }];

                    } andError:^(NSError *error) {
                        
                    } ShowHUD:YES];
                }
        
            } else if ([text isEqual: _cityTextField]) {
                if ([_typeTextField.text isEqualToString:@""]) {
                    HQJLog(@"你还没有选择服务商");
                } else if ([_provincesTextField.text isEqualToString:@""]) {
                    HQJLog(@"你还没选择省份");
                } else {
                    if (text.frame.origin.y+text.frame.size.height-_offSizeScroll+100 >HEIGHT) {
                        CGFloat offset = text.frame.origin.y+text.frame.size.height-_offSizeScroll+100 - HEIGHT;
                        self.mainScrollView.contentOffset = CGPointMake(0, self.mainScrollView.contentOffset.y+offset+20);
                    }
                    
                    [RequestEngine HQJFacilitatorRequestDetailsUrl:[NSString stringWithFormat:@"%@getCityByAjax/province/%@",AppOrg_URL,_provincesAry[_provincesIndex][@"identityid"]] complete:^(NSDictionary *dic) {
                        _cityAry = (NSArray *)dic[@"result"];
                        NSMutableArray *arrays = [NSMutableArray array];
                        for (NSInteger i=0; i<_cityAry.count; i++) {
                            NSString *str = _cityAry[i][@"area"];
                            
                            [arrays addObject:str];
                        }
                        _pickView = [[pickerView alloc]initWithFrame:CGRectMake(text.frame.origin.x, text.frame.origin.y+text.frame.size.height-_offSizeScroll, text.frame.size.width, 100) andTitleAry:arrays];
                        [_pickView showView];
                        [_pickView setSenderBlock:^(id sender) {
                            
//                            HQJLog(@"%@-=-=-",arrays[[sender integerValue]]);
                            if (![text.text isEqualToString:arrays[[sender integerValue]]]) {
                                self_weak_.countyTextField.text = @"";
                            }
                            text.text = [NSString stringWithFormat:@"%@",arrays[[sender integerValue]]];
                            _cityIndex = [sender integerValue];
                            
                        }];
                        
                    } andError:^(NSError *error) {
                        
                    } ShowHUD:YES];
                }
                
            }else if ([text isEqual: _countyTextField]) {
          
                if ([_typeTextField.text isEqualToString:@""]) {
                    [ManagerEngine homeSvpStr:@"你还没有选择服务商" andcenterView:CustomWindow andStyle:promptViewFadeAway];
//                    HQJLog(@"你还没有选择服务商:%@",_typeTextField.text);
                } else if ([_provincesTextField.text isEqualToString:@""]) {
                    [ManagerEngine homeSvpStr:@"你还没选择省份" andcenterView:CustomWindow andStyle:promptViewFadeAway];
//                    HQJLog(@"你还没选择省份:%@",_provincesTextField.text);
                }else if ([_cityTextField.text isEqualToString:@""]){
                    [ManagerEngine homeSvpStr:@"你还没选择城市" andcenterView:CustomWindow andStyle:promptViewFadeAway];
//                    HQJLog(@"你还没选择城市:%@",_cityTextField.text);
                }
                else {
                    if (text.frame.origin.y + text.frame.size.height - _offSizeScroll+100 > HEIGHT) {
                        CGFloat offset = text.frame.origin.y+text.frame.size.height-_offSizeScroll+100 - HEIGHT;
                        self.mainScrollView.contentOffset = CGPointMake(0, self.mainScrollView.contentOffset.y+offset+20);
                    }
                    NSLog(@"%@",[NSString stringWithFormat:@"%@getCountyByAjax/city/%@/deposit/%@",AppOrg_URL,_cityAry[_cityIndex][@"identityid"],_typeAry[_typeIndex][@"typename"]]);
                    NSString *url = [NSString stringWithFormat:@"%@getCountyByAjax/city/%@/deposit/%@",AppOrg_URL,_cityAry[_cityIndex][@"identityid"],_typeAry[_typeIndex][@"typeid"]];

                    [RequestEngine HQJFacilitatorRequestDetailsUrl:url complete:^(NSDictionary *dic) {
                      
                        if([dic[@"error"]floatValue] == 0) {
                      
                        _countyAry = (NSArray *)dic[@"result"];
                        NSMutableArray *arrays = [NSMutableArray array];
                        for (NSInteger i=0; i<_countyAry.count; i++) {
                            NSString *str = _countyAry[i][@"area"];
                            
                            [arrays addObject:str];
                        }
                        _pickView = [[pickerView alloc]initWithFrame:CGRectMake(text.frame.origin.x, text.frame.origin.y+text.frame.size.height-_offSizeScroll, text.frame.size.width, 100) andTitleAry:arrays];
                        [_pickView showView];
                        [_pickView setSenderBlock:^(id sender) {
                            
                            
                            text.text = [NSString stringWithFormat:@"%@",arrays[[sender integerValue]]];
                            _countyIndex = [sender integerValue];
                           
                        }];
                            
                        } else if ([dic[@"error"]floatValue] == 40099) {
                            
                            [ManagerEngine homeSvpStr:@"已无可选区域" andcenterView:CustomWindow andStyle:promptViewFadeAway];
                        } else {
                            
                                 [ManagerEngine homeSvpStr:dic[@"result"][@"errmsg"] andcenterView:CustomWindow andStyle:promptViewFadeAway];
                        }
                    } andError:^(NSError *error) {
                        
                    } ShowHUD:YES];
                }
                
                
            } else {
                if (text.frame.origin.y+text.frame.size.height-_offSizeScroll+100 >HEIGHT) {
                    CGFloat offset = text.frame.origin.y+text.frame.size.height-_offSizeScroll+100 - HEIGHT;
                    self.mainScrollView.contentOffset = CGPointMake(0, self.mainScrollView.contentOffset.y+offset+20);
                }
                
                [RequestEngine HQJFacilitatorRequestDetailsUrl:[NSString stringWithFormat:@"%@getDeposit",AppOrg_URL] complete:^(NSDictionary *dic) {
                    _typeAry = (NSArray *)dic[@"result"];
                    NSMutableArray *arrays = [NSMutableArray array];
                    for (NSInteger i=0; i<_typeAry.count; i++) {
                        NSString *str = _typeAry[i][@"typename"];

                        [arrays addObject:str];
                    }
                    _pickView = [[pickerView alloc]initWithFrame:CGRectMake(text.frame.origin.x, text.frame.origin.y+text.frame.size.height-_offSizeScroll, text.frame.size.width, arrays.count*30) andTitleAry:arrays];

                    [_pickView showView];
                    [_pickView setSenderBlock:^(id sender) {
                         _typeIndex = [sender integerValue];
//                         HQJLog(@"输入完成后更改:%@\n:%@",text.text,[NSString stringWithFormat:@"%@",arrays[[sender integerValue]]]);
                        if (![text.text isEqualToString:[NSString stringWithFormat:@"%@",arrays[[sender integerValue]]]]) {
                            self_weak_.provincesTextField.text = @"";
                            self_weak_.cityTextField.text = @"";
                            self_weak_.countyTextField.text = @"";
                        }

                        text.text = [NSString stringWithFormat:@"%@",arrays[[sender integerValue]]];
                       
//

                    }];
                   
                } andError:^(NSError *error) {
                    
                } ShowHUD:YES];
            }
           
        } forControlEvents:UIControlEventTouchUpInside];
    
   
    btn.frame =CGRectMake(0, 0, 20, 20);
    [btn setImage:[UIImage imageNamed:@"DownArrow"] forState:UIControlStateNormal];
    text.rightView = btn;
    text.rightViewMode = UITextFieldViewModeAlways;
  
}


#pragma mark --
#pragma mark ---Scrolll Delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    _offSizeScroll =scrollView.contentOffset.y;
}

#pragma mark --
#pragma mark ---信号创建
-(void)createSignal{
    RACSignal *mobileSignal = [_mobileTextField.rac_textSignal map:^id(id value) {
        return @([ManagerEngine valiMobile:value]);
    }];
    
//    RACSignal *nameSignal = [_nameTextField.rac_textSignal map:^id(id value) {
//        return @([ManagerEngine isNameValid:value]);
//    }];
//    
//    RACSignal *idSignal = [_idTextField.rac_textSignal map:^id(id value) {
//        return @([ManagerEngine validateIdentityCard:value]);
//    }];
    
    RACSignal *emailSignal = [_emailTextField.rac_textSignal map:^id(id value) {
        return @([ManagerEngine validateEmail:value]);
    }];
    
    RACSignal *addessSignal = [_addressTextField.rac_textSignal map:^id(id value) {
        return @([self isValiAddess:value]);
    }];
    
//    RACSignal *ReferrerSignal = [_ReferrerTextField.rac_textSignal map:^id(id value) {
//        return @([ManagerEngine valiMobile:value]);
//    }];
    
    RACSignal *transactorSignal = [_transactorTextField.rac_textSignal map:^id(id value) {
//        HQJLog(@"transactorSignal:%@",@([ManagerEngine valiMobile:value]))

        return @([ManagerEngine valiMobile:value]);
    }];
    
    RACSignal *servicerSignal = [_servicerTextField.rac_textSignal map:^id(id value) {
//        HQJLog(@"servicerSignal:%@",@([ManagerEngine valiMobile:value]))

        return @([ManagerEngine valiMobile:value]);
    }];
    
    RACSignal *organiserSignal = [_organiserTextField.rac_textSignal map:^id(id value) {
//        HQJLog(@"organiserSignal:%@",@([ManagerEngine valiMobile:value]))

        return @([ManagerEngine valiMobile:value]);
    }];
//    RACSignal *countySignal = [_countyTextField.rac_textSignal map:^id(id value) {
//                HQJLog(@"countySignal:%@",value)
//        
//        return @(_isCounty);
//    }];
//  countyTextField
   
    [mobileSignal subscribeNext:^(NSNumber * x) {
        if ([x boolValue]) {
            [self getVipInformationMoile:_mobileTextField.text];
        } else {
            
            _nameTextField.text =@"";
            _idTextField.text =@"";
            _emailTextField.text =@"";
            _addressTextField.text =@"";
        }
    }];
    
    
    [transactorSignal subscribeNext:^(NSNumber *x) {
        if ([x boolValue]) {
            [self getName:_transactorTextField];
            
        } else {
            _transactorLabel.text =@"";
            [self uplayout];
        }
    }];
    [servicerSignal subscribeNext:^(NSNumber *x) {
        if ([x boolValue]) {
            [self getName:_servicerTextField];
            
        }else {
            _servicerLabel.text = @"";
            [self uplayout];

        }
    }];
    [organiserSignal subscribeNext:^(NSNumber *x) {
        if ([x boolValue]) {
            [self getName:_organiserTextField];
            
        }else {
            _organiserLabel.text = @"";
            [self uplayout];

        }
    }];
    
//    [RACObserve(_transactorLabel, text) subscribeNext:^(NSString *str) {
//        HQJLog(@"不知道输入的什么%ld",str.length);
//    }];
    RACSignal *upSignal = [RACSignal combineLatest:@[mobileSignal,emailSignal,addessSignal,transactorSignal,servicerSignal,organiserSignal] reduce:^id(NSNumber *mobileValid,NSNumber *emailValid,NSNumber *addessValid,NSNumber *transactorValid,NSNumber *servicerValid,NSNumber *organiserValid){
        
        return @([mobileValid boolValue]&&[emailValid boolValue]&&[addessValid boolValue]&&[transactorValid boolValue]&&[servicerValid boolValue]&&[organiserValid boolValue]);
    }];
    [RACObserve(_countyTextField, text)subscribeNext:^(id x) {
        HQJLog(@"最后一个文本框的:%@",x);
        if (x !=nil) {
            [self nilEmpty];
            
            _logonButton.enabled = _isCounty;
            HQJLog(@"总信号处理:%@",@(_isCounty));
            
            if (_isCounty) {
                _logonButton.backgroundColor = [ManagerEngine getColor:@"00ccb8"];
                
            } else {
                _logonButton.backgroundColor = [ManagerEngine getColor:@"8ae6dc"];
                
            }

            
            //            _isCounty = YES;
        }
    }];
    [upSignal subscribeNext:^(NSNumber *mergeSignal) {
        
        _isAllSignal = [mergeSignal boolValue];
      

    }];
    
    
    [RACObserve(self,isAllSignal)subscribeNext:^(NSNumber *sender) {
      
        if (_isCounty) {
       
        _logonButton.enabled = [sender boolValue];
        HQJLog(@"假装我是总信号处理:%@\n%@",sender,@(_isCounty));
        
        if ([sender boolValue]) {
            _logonButton.backgroundColor = [ManagerEngine getColor:@"00ccb8"];
            
        } else {
            _logonButton.backgroundColor = [ManagerEngine getColor:@"8ae6dc"];
            
        }
        
            
        }
        
    }];
    [[_logonButton rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(id x) {
        if ([self.organiserTextField.text isEqualToString:self.servicerTextField.text]){
            [ManagerEngine homeSvpStr:@"服务人和组织人不能相同" andcenterView:CustomWindow andStyle:promptViewFadeAway];
        }else {
            [self allRequstr];

        }
    }];
    
}
-(BOOL)isValiAddess:(NSString *)text {
    
    if (text.length>0) {
        return YES;
    } else {
        return NO;
    }
    
}
-(void)nilEmpty {
    NSLog(@"_nameTextField.text:%@\n_idTextField.text:%@\n_ReferrerTextField.text:%@\n_countyTextField.text:%@",_nameTextField.text,_idTextField.text,_ReferrerTextField.text,_countyTextField.text);
    
    if (![_nameTextField.text isEqualToString:@""]
        &&![_idTextField.text isEqualToString:@""]&&
        ![_ReferrerTextField.text isEqualToString:@""]&&
        ![_countyTextField.text isEqualToString:@""] ){
        _isCounty = YES;
        
    } else {
        _isCounty = NO;
    }
}
-(void)textFieldDidEndEditing:(UITextField *)textField {
    if (textField == _ReferrerTextField) {
        
    }
    
    
    
    
}



#pragma mark --
#pragma mark --- 获取执行人 ,服务人 等 信息
-(void)getName:(UITextField  *)tit {
    [RequestEngine HQJFacilitatorRequestDetailsUrl:[NSString stringWithFormat:@"%@checkOrgByAjax/originatortype/servicer/mobile/%@",AppOrg_URL,tit.text] complete:^(NSDictionary *dic) {
        
//        HQJLog(@"输入完手机号获取的信息:%@",dic);
        if ([dic[@"error"] integerValue]==0) {
            if (tit == _transactorTextField) {
                _transactorLabel.text = dic[@"result"][@"realname"];
                _transactorIDStr = dic[@"result"][@"memberid"];
            } else if (tit == _servicerTextField) {
                _servicerLabel.text = dic[@"result"][@"realname"];
                _servicerIDStr = dic[@"result"][@"memberid"];
            }else {
                _organiserLabel.text = dic[@"result"][@"realname"];
                _organiserIDStr = dic[@"result"][@"memberid"];
            }
            [self uplayout];
        } else if([dic[@"error"] integerValue] == 48001){
            
            
            [ManagerEngine homeSvpStr:@"无数据" andcenterView:CustomWindow andStyle:promptViewFadeAway];
        }else if([dic[@"error"] integerValue] == 48002){
            [ManagerEngine homeSvpStr:@"无匹配电话号码" andcenterView:CustomWindow andStyle:promptViewFadeAway];
        }else if([dic[@"error"] integerValue] == 48003){
            [ManagerEngine homeSvpStr:@"该服务商无此资质" andcenterView:CustomWindow  andStyle:promptViewFadeAway];
        } else {
            [ManagerEngine homeSvpStr:@"参数不全" andcenterView:CustomWindow andStyle:promptViewFadeAway];
        }
        
        
    } andError:^(NSError *error) {
        
    } ShowHUD:NO];
    
}

#pragma mark --
#pragma mark ---获取当前会员的信息
-(void)getVipInformationMoile:(NSString *)str {
    
    [RequestEngine HQJFacilitatorRequestDetailsUrl:[NSString stringWithFormat:@"%@checkCusInfo/mobile/%@",AppOrg_URL,str] complete:^(NSDictionary *dic) {
        if ([dic[@"error"] integerValue] == 0) {
            _nameTextField.text = dic[@"result"][@"realname"];
            _idTextField.text = dic[@"result"][@"identitynum"];
            NSString *emailStr = [NSString stringWithFormat:@"%@",dic[@"result"][@"email"]];
            NSString *addressStr = [NSString stringWithFormat:@"%@",dic[@"result"][@"address"]];
            if (![emailStr isEqualToString:@"<null>"]) {
               _emailTextField.text =emailStr ;

            }
            if (![addressStr isEqualToString:@"<null>"]) {
                _addressTextField.text = addressStr;

            }
//            HQJLog(@"第一个输入框输入完返回的东西:%@\n%@",emailStr,addressStr);

//            if (![dic[@"result"][@"email"] isKindOfClass:[NSString class]]) {
//            }
//            if(![dic[@"result"][@"address"] isKindOfClass:[NSString class]]){
//            }
        } else if ([dic[@"error"] integerValue]==40062 ) {
            [ManagerEngine homeSvpStr:@"该用户暂不能成为服务商" andcenterView:CustomWindow andStyle:promptViewFadeAway];
        } else if ([dic[@"error"] integerValue]==40061 ){
            
            [ManagerEngine homeSvpStr:@"数据错误" andcenterView:CustomWindow andStyle:promptViewFadeAway];
        } else if ([dic[@"error"] integerValue]==40063 ){
            [ManagerEngine homeSvpStr:@"该电话已被注册成服务商" andcenterView:CustomWindow andStyle:promptViewFadeAway];
            
        }else if ([dic[@"error"] integerValue]==40065 ){
            
             [ManagerEngine homeSvpStr:@"无匹配电话号码" andcenterView:CustomWindow andStyle:promptViewFadeAway];
        } else {
             [ManagerEngine homeSvpStr:@"参数不全" andcenterView:CustomWindow andStyle:promptViewFadeAway];
        }
        
    } andError:^(NSError *error) {
        
    } ShowHUD:NO];
    
}


//-(void)viewDidLayoutSubviews {
//    [super viewDidLayoutSubviews];
//   
//}
-(void)uplayout {
    
    CGFloat nameOneSize = [ManagerEngine setTextWidthStr:_transactorLabel.text andFont:[UIFont systemFontOfSize:15]];
    CGFloat nameTwoSize = [ManagerEngine setTextWidthStr:_servicerLabel.text andFont:[UIFont systemFontOfSize:15]];
    CGFloat nameThreeSize = [ManagerEngine setTextWidthStr:_organiserLabel.text andFont:[UIFont systemFontOfSize:15]];
    _transactorHeaderView.sd_layout.leftSpaceToView(self.mainScrollView,WIDTH-15-nameOneSize-20-5).topSpaceToView(_transactorTextField,5).heightIs(20).widthIs(20);
    _transactorLabel.sd_layout.leftSpaceToView(_transactorHeaderView,5).topEqualToView(_transactorHeaderView).heightIs(20).widthIs(nameOneSize);
    _servicerHeaderView.sd_layout.leftSpaceToView(self.mainScrollView,WIDTH-15-nameTwoSize-20-5).topSpaceToView(_servicerTextField,5).heightIs(20).widthIs(20);
    _servicerLabel.sd_layout.leftSpaceToView(_servicerHeaderView,5).topEqualToView(_servicerHeaderView).heightIs(20).widthIs(nameTwoSize);
    
    _organiserHeaderView.sd_layout.leftSpaceToView(self.mainScrollView,WIDTH-15-nameThreeSize-20-5).topSpaceToView(_organiserTextField,5).heightIs(20).widthIs(20);
    _organiserLabel.sd_layout.leftSpaceToView(_organiserHeaderView,5).topEqualToView(_organiserHeaderView).heightIs(20).widthIs(nameThreeSize);

    
//    _transactorLabel
//    _servicerLabel
//    _organiserLabel
//    HeaderView.frame = CGRectMake(WIDTH-15-nameSize.width-20-5,i%4*(20+60)+_ReferrerTextField.frame.origin.y+_ReferrerTextField.frame.size.height+5, 20, 20);
//    nameStrLabel.sd_layout.leftSpaceToView(HeaderView,5).topEqualToView(HeaderView).heightIs(20).widthIs(nameSize.width);

}

-(void)headerView:(UIImageView *)image {
    [self.mainScrollView addSubview:image];
    image.image = [UIImage imageNamed:@"man"];
    
}
-(void)headerLabel:(UILabel *)label {
    label.font = [UIFont systemFontOfSize:15];
    label.textColor = [UIColor redColor];
    [self.mainScrollView addSubview:label];
    
}


#pragma mark --
#pragma mark --- 最后的注册请求
-(void)allRequstr {
    if (!_isAllSignal) {
       
        [ManagerEngine homeSvpStr:@"请完善信息后重试" andcenterView:CustomWindow andStyle:promptViewFadeAway];
    } else {
        NSString *urlStr = [NSString stringWithFormat:@"%@originatorAddAction/mobile/%@/realname/%@/identitynum/%@/email/%@/address/%@/developer/%@/servicer/%@/executor/%@/manager/%@/deposit/%@/areacounty/%@/",AppOrg_URL,_mobileTextField.text,_nameTextField.text,_idTextField.text,_emailTextField.text,_addressTextField.text,[FileEngine filePathNameReadName:@"memberid" andstye:fileLoginStyle],_transactorIDStr,_servicerIDStr,_organiserIDStr,_typeAry[_typeIndex][@"typeid"],_countyAry[_countyIndex][@"identityid"]];
        NSString *enCodeStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        HQJLog(@"%@\n%@\n%@",urlStr,@(_isAllSignal),@(_isCounty));

        [RequestEngine HQJFacilitatorRequestDetailsUrl:enCodeStr complete:^(NSDictionary *dic) {
            if([dic[@"error"]integerValue] == 0) {
                SUCCESSViewController *SUVC =[[SUCCESSViewController alloc]init];
                SUVC.title = @"服务商注册";
                [self.navigationController pushViewController:SUVC animated:YES];
                
                
                
            } else {
                  [ManagerEngine homeSvpStr:dic[@"result"][@"errmsg"] andcenterView:CustomWindow andStyle:promptViewFadeAway];
               
            }
        } andError:^(NSError *error) {
            
        } ShowHUD:YES];
    }
    

    
    
}

#pragma mark --
#pragma mark --- 进来判断能否注册
-(void)firstRequst {
    NSString *urlStr =[NSString stringWithFormat:@"%@originatorReg/memberid/%@",AppOrg_URL,MmberidStr];
    [RequestEngine HQJFacilitatorRequestDetailsUrl:urlStr complete:^(NSDictionary *dic) {
        HQJLog(@"有几个服务商了：%@",dic);
        switch ([dic[@"error"]integerValue]) {
            case 47007:
                [ManagerEngine customAlert:@"恭喜您，当前已达到注册服务商上限人数" andViewController:self isBack:YES];
                break;
            case 47008:
                [ManagerEngine customAlert:@"参数不全" andViewController:self isBack:YES];
                break;
            default:
                break;
        }
    } andError:^(NSError *error) {
        
    } ShowHUD:YES];
    
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
